package homeWork_10_1.Exam1_2;

import dbTest.Util.DBUtil;
import dbTest.Util.PrepareStatementUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Suppermacket {
    public ArrayList<Goods> check(){
        ArrayList<Goods> als = new ArrayList<>();
        String sql = "select * from goods where flag = ?";
        Object excute = PrepareStatementUtil.excute(sql, 1);
        ResultSet rs = (ResultSet) excute;
        try {
            while(rs.next()){
                als.add(
                new Goods(rs.getString(1),rs.getInt(2)
                ,rs.getString(3),rs.getInt(4)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            PrepareStatementUtil.close();
            DBUtil.close();
        }


        return als;
    }

    public Goods query(String name){
        Goods g = null;
        String sql = "select * from goods where name like ?";
        String n = "%"+name+"%";
        Object excute = PrepareStatementUtil.excute(sql, n);
        ResultSet rs = (ResultSet) excute;
        try {
            if (rs.next()){
                g = new Goods(rs.getString(1),rs.getInt(2)
                                ,rs.getString(3),rs.getInt(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            PrepareStatementUtil.close();
            DBUtil.close();
        }
        return g;
    }


}
